layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/marketBrand/jsonList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {type: 'numbers', title: '序号'},
            {field: 'title', title: '名称',align:'center'},
            {field: 'marketTitle', title: '营销名称',align:'center'},
            {field: 'showPic', title: '图片',align:'center',width:100, templet:function(d){
                return "<img src='"+d.showPic+"' style='width: 80px;height: 80px;'/>";
            }},
            {field: 'url', title: '链接',align:'center'},
            {field: 'status', title: '状态', align:'center', minWidth:110, templet:function(d){
                if(d.status=='1'){
                    return '有效';
                }else {
                    return '无效';
                }
            }},
            {field: 'sortOrder', title: '排序',align:'center'},
            {title: '操作',width:170,align:"center", templet:function (d) {
                return '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>'+
                    '<a class="layui-btn layui-btn-xs" lay-event="del">删除</a>';
            }}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                title: $(".searchVal").val(),
                marketId : $("#marketId").val()
            }
        })
    });

    //增加/编辑
    function addEntity(params){
        var title="编辑";
        if(params==null||params==""){
            title="添加"
        }
        var index = layui.layer.open({
            title :title,
            type : 2,
            area: ['430px', '520px'], //宽高
            content : "/marketBrand/selectEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                layui.layer.full(index);

            }
        })

    }
    $(".add_btn").click(function(){
        addEntity('');
    })

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addEntity("?id="+data.id);
        }else if(layEvent === 'del'){ //删除
            layer.confirm('是否删除该营销品牌？',{icon:3, title:'提示信息'},function(index){
                 $.get("/marketBrand/deleteEntity",{
                     "id":data.id,
                     "_method":"DELETE"
                 },function(result){
                     if(1==result.returnInfo.state){
                         tableIns.reload();
                         layer.close(index);
                     }else{
                         top.layer.msg(result.returnInfo.errorMsg);
                     }
                 })
            });
        }
    });

})